from typing import List

from tortoise.transactions import atomic

from backend.app.crud.base import CRUDBase
from backend.app.models.goods import Goods
from backend.app.schemas.goods import CreateGoodsSchema, UpdateGoodsSchema


class CRUDGoods(CRUDBase[Goods, CreateGoodsSchema, UpdateGoodsSchema]):
    async def get_by_id(self, pk) -> Goods:
        return await self.model.filter(id=pk).first()

    @atomic()
    async def update_vector_id(self, pk: int, vector_id: str) -> int:
        return await self.model.filter(id=pk).update(vector_id=vector_id)

    @atomic()
    async def batch_delete_vector_id(self, goods_ids: List[int]) -> int:
        return await self.model.filter(id__in=goods_ids).update(vector_id="")


GoodsDao: CRUDGoods = CRUDGoods(Goods)
